from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

from flask import Flask, session, app, logging

from info import db, create_app
from info.models import User

"""通过制定方法创建对应配置的APP  create_app类似于工厂方法"""
app = create_app('development')

"""用 flask_script接管 python代码"""
manager = Manager(app)
#必须用命令才能  使代码运行  ，可以设置edit configuration ——————runserver点击右键运行
"""将 app 与  db 关联起来"""
Migrate(app,db)
"""将迁徙命令 添加到manager中"""
manager.add_command('db',MigrateCommand)



# TODO 创建超级管理员账号 密码
@manager.option('-n', '-name', dest="name")
@manager.option('-p', '-password', dest="password")
def createsuperuser(name,password):

    if not all([name,password]):
        print("参数不够")
    # 初始化 用户表
    user = User()
    user.nick_name = name
    user.mobile = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        print(e)
    print("添加超级管理员账号成功")

# @app.route('/')
# def index():
#     # session["name"] = "xioabing"
#
#     """"打印日志文件"""
#     logging.debug('测试debug')
#     logging.warning('测试warning')
#     logging.error("测试error")
#     logging.fatal('测试fatal')
#     return 'index222'

if __name__ == '__main__':
    manager.run()